package cn.jy.careuplusdoctor.utils;

import java.io.File;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.Toast;
import cn.jy.careuplusdoctor.R;


public class PopupWindowCommon {
	
	/**
	 * 回调CODE:调用相机
	 */
	public static final int	REQUEST_CODE_CAMERA = 9011;
	/**
	 * 回调CODE:调用图片选择
	 */
	public static final int	REQUEST_CODE_CHOOSE_IMG	= 9012;
	/**
	 * 回调CODE:调用图片加截图选择
	 */
	public static final int	REQUEST_CODE_CHOOSE_CUT_IMG	= 9013;
	
	private Activity activity;
	
	private View view,btnCamera,btnChooseImg;
	
	public View getView() {
		return view;
	}
	public void setView(View view) {
		this.view = view;
	}

	private PopupWindow popupWindow;
	
	public Activity getActivity() {
		return activity;
	}
	public void setActivity(Activity activity) {
		this.activity = activity;
	}
	public PopupWindowCommon(Activity activity){
		setActivity(activity);
	}
	/**
	 * 初始化化图版选择层
	 * @return
	 */
	public PopupWindowCommon initChooseImg(){
		LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.window_add_image, null);
		btnCamera = view.findViewById(R.id.btnCamera);
		btnChooseImg = view.findViewById(R.id.btnChooseImg);
		popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		popupWindow.setBackgroundDrawable(new ColorDrawable());
		popupWindow.setOutsideTouchable(true);
		btnCamera.setOnClickListener(btnCameraOnClickListener);
		btnChooseImg.setOnClickListener(btnChooseImgOnClickListener);
		return this;
	}
	
	public PopupWindowCommon initLoading(String text){
		LoadingCommon loadingCommon;
		if(StringUtil.isNotEmpty(text)){
			loadingCommon = new LoadingCommon(getActivity(),text);
		}else{
			loadingCommon = new LoadingCommon(getActivity());
		}
		view = loadingCommon.getLoadingLayout();
		view.setBackgroundColor(Color.BLACK);
		view.getBackground().setAlpha(70);
		loadingCommon.getLoadingTxt().setTextColor(Color.WHITE);
		popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		popupWindow.setBackgroundDrawable(new ColorDrawable());
		popupWindow.setOutsideTouchable(true);
		return this;
	}
	
	/**
	 * 显示图片选择层
	 * @param parentView
	 */
	public void showChooseImg(View parentView){
		if(parentView==null){
			return;
		}
		popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
	}
	
	/**
	 * 显示图片选择层
	 * @param parentView
	 */
	public void showLoading(View parentView){
		if(parentView==null){
			return;
		}
		popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
	}
	
	public boolean isShowing(){
		return popupWindow.isShowing();
	}
	
	public void hide(){
		popupWindow.dismiss();
	}
	
	OnClickListener btnCameraOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View paramView) {
			String status=Environment.getExternalStorageState(); 
			if (status.equals(Environment.MEDIA_MOUNTED)) {
				try {
					String fileName = "temp.jpg";
					File dir = new File(CheckNewVersion.FILE_SAVE_DIR);
					if (!dir.exists()){
						dir.mkdirs();
					}
					Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
					File f = new File(dir, fileName);// localTempImgDir和localTempImageFileName是自己定义的名字
					Uri u = Uri.fromFile(f);
					intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
					getActivity().startActivityForResult(intent, REQUEST_CODE_CAMERA);
				} catch (ActivityNotFoundException e) {
					Toast.makeText(getActivity(), "没有找到储存目录", Toast.LENGTH_SHORT).show();
					
				}
			} else {
				Toast.makeText(getActivity(), "没有储存卡", Toast.LENGTH_SHORT).show();
			}
		}
	};
	
	OnClickListener btnChooseImgOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View paramView) {
			Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
			getActivity().startActivityForResult(intent, REQUEST_CODE_CHOOSE_IMG);
		}
	};
	
}
